package com.example.zce.back.controller.index;

import com.alibaba.fastjson.JSON;
import com.example.zce.back.common.constant.REDIS_KEY_CONSTANT;
import com.example.zce.back.common.redis.RedisService;
import com.example.zce.library.data.sys.model.BackMenuModel;
import com.example.zce.library.data.sys.service.BackMenuService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 描述：首页相关
 *
 * @author zengyz
 * @date 2018/7/25 15:29
 */
@Controller
public class IndexController {

    @Autowired
    private BackMenuService backMenuService;

    @Autowired
    private RedisService redisService;

    /**
     * 首页
     * @return
     */
    @GetMapping({"/","index.html"})
    public ModelAndView indexHtml(){
        ModelAndView modelAndView=new ModelAndView();
        String  menuTreeStr = (String)redisService.get(REDIS_KEY_CONSTANT.MENU_TREE);
        if (StringUtils.isNotEmpty(menuTreeStr)){
            List<BackMenuModel> backMenuModels = JSON.parseArray(menuTreeStr, BackMenuModel.class);
            modelAndView.addObject("backMenuList",backMenuModels);
        }else{
            List<BackMenuModel> backMenuTree = backMenuService.getBackMenuTree();
            modelAndView.addObject("backMenuList",backMenuTree);
            redisService.set(REDIS_KEY_CONSTANT.MENU_TREE, JSON.toJSONString(backMenuTree));
        }
        modelAndView.setViewName("index");
        return modelAndView;
    }

    /**
     * 欢迎页
     * @return
     */
    @GetMapping("/welcome.html")
    public ModelAndView welcomeHtml(){
        return new ModelAndView("welcome");
    }
}
